package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import log.MyLogger;
import model.service.Employee;
import model.service.ServiceAction;
import model.service.ServiceActionStatusType;
import model.service.ServiceDevice;
import model.service.ServiceOrder;


public class ServiceActionDAOImpl {

	String sql = "" ;
	db.dbconnection dbconnection ;
	java.sql.Connection myConnection ;
	Statement st ;
	
	public List<ServiceAction> getServiceActions(int serviceOrder) {
		List<ServiceAction> serviceActions = new ArrayList<ServiceAction>();
		
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM service_action WHERE service_order_fk = " + serviceOrder;
			
			ResultSet r = st.executeQuery(sql);
			ServiceActionStatusTypeDAOImpl serviceActionStatusTypeDAOImpl = new ServiceActionStatusTypeDAOImpl();
			ServiceTypeDAOImpl serviceTypeDAOImpl  = new ServiceTypeDAOImpl();
			
			while (r.next()) {
				ServiceAction sa = new ServiceAction();
				sa.setServiceAction(r.getInt("service_action"));
				sa.setServiceOrder(new ServiceOrder(r.getInt("service_order_fk")));
				sa.setServiceDevice(new ServiceDevice(r.getInt("service_device_fk"))); 
				sa.setActionDescription(r.getString("action_description"));
				sa.setPrice(r.getFloat("price"));
				sa.setCreated(r.getTimestamp("created"));
				sa.setCreatedBy(new Employee(r.getInt("created_by"))); 
				ServiceActionStatusType serviceActionStatusType = new ServiceActionStatusType(r.getInt("service_action_status_type_fk"));
				serviceActionStatusType.setTypeName(serviceActionStatusTypeDAOImpl.getTypeNameByServiceActionStatusTypeId(serviceActionStatusType.getServiceActionStatusType()));
				sa.setServiceActionStatusType(serviceActionStatusType);
				sa.setServiceType(serviceTypeDAOImpl.getServicType(r.getInt("service_type_fk")));
				sa.setServiceAmount(r.getFloat("service_amount"));
				sa.setPriceUpdated(r.getTimestamp("price_updated"));
						
				serviceActions.add(sa);
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.getServiceActions()", e.getMessage());
		}
		return serviceActions;
	}

	public boolean insertServiceAction(ServiceAction newAction) {
		try {
			dbconnection = new db.dbconnection();
			myConnection= dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "INSERT INTO service_action(service_action_status_type_fk, service_type_fk, service_device_fk,service_order_fk,service_amount,price,price_updated,action_description,created,created_by)" +
					" VALUES("+newAction.getServiceActionStatusType().getServiceActionStatusType()+","+newAction.getServiceType().getServiceType()+","+newAction.getServiceDevice().getServiceDevice()+","+newAction.getServiceOrder().getServiceOrder()+","+newAction.getServiceAmount()+","+newAction.getPrice()+",NOW(),'"+newAction.getActionDescription()+"',NOW(),"+newAction.getCreatedBy().getEmployee()+")";
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.insertServiceAction()", e.getMessage());
			return false;
		}
		return true;
		
	}

	public List<ServiceActionStatusType> getServiceActionStatusTypes() {
		List<ServiceActionStatusType> t = new ArrayList<ServiceActionStatusType>();
		try {
			Connection c = dbconnection.getConnection();
			Statement st = c.createStatement();
			String sql = "select * from service_action_status_type";
			ResultSet r = st.executeQuery(sql);
			while(r.next()){
				ServiceActionStatusType serviceActionStatusType = new ServiceActionStatusType(r.getInt("service_action_status_type"));
				serviceActionStatusType.setTypeName(r.getString("type_name"));
				t.add(serviceActionStatusType);
			}
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.insertServiceAction()", e.getMessage());
			return null;
		}
		return t;
	}

	public ServiceAction getserviceActionById(int serviceActionId) {
		ServiceAction _return = null;
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM service_action WHERE service_action = " + serviceActionId;
			
			ResultSet r = st.executeQuery(sql);
			ServiceActionStatusTypeDAOImpl serviceActionStatusTypeDAOImpl = new ServiceActionStatusTypeDAOImpl();
			ServiceTypeDAOImpl serviceTypeDAOImpl  = new ServiceTypeDAOImpl();
			
			
			while (r.next()) {
				ServiceAction sa = new ServiceAction();
				sa.setServiceAction(r.getInt("service_action"));
				sa.setServiceOrder(new ServiceOrder(r.getInt("service_order_fk")));
				sa.setServiceDevice(new ServiceDevice(r.getInt("service_device_fk"))); 
				sa.setActionDescription(r.getString("action_description"));
				sa.setPrice(r.getFloat("price"));
				sa.setCreated(r.getTimestamp("created"));
				sa.setCreatedBy(new Employee(r.getInt("created_by"))); 
				ServiceActionStatusType serviceActionStatusType = new ServiceActionStatusType(r.getInt("service_action_status_type_fk"));
				serviceActionStatusType.setTypeName(serviceActionStatusTypeDAOImpl.getTypeNameByServiceActionStatusTypeId(serviceActionStatusType.getServiceActionStatusType()));
				sa.setServiceActionStatusType(serviceActionStatusType);
				sa.setServiceType(serviceTypeDAOImpl.getServicType(r.getInt("service_type_fk")));
				sa.setServiceAmount(r.getFloat("service_amount"));
				sa.setPriceUpdated(r.getTimestamp("price_updated"));
				_return = sa;
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.getserviceActionById()", e.getMessage());
			return null;
		}
		return _return;
	}

	public boolean updateServiceAction(ServiceAction serviceAction) {
		try {
			dbconnection = new db.dbconnection();
			myConnection= dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			//peab muutma veel see esialgne
			String sql = "UPDATE service_action SET service_type_fk="+serviceAction.getServiceType().getServiceType()+", service_amount="+serviceAction.getServiceAmount()+", price="+serviceAction.getPrice()+" WHERE service_action="+serviceAction.getServiceAction();
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.updateServiceAction()", e.getMessage());
			return false;
		}
		return true;
	}

	public boolean deleteServiceAction(int service_action) {
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			//peab muutma veel see esialgne
			String sql = "DELETE FROM service_action WHERE service_action="+service_action;
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceActionDAOImpl.deleteServiceAction()", e.getMessage());
			return false;
		}
		return true;
	}
}
